package cn.bdqn.controller.questionBank;

import cn.bdqn.dao.questionBank.TestPaperTypeDao;
import cn.bdqn.dto.LoginUser;
import cn.bdqn.dto.questionBank.test_paper.TestPaperInsertDTO;
import cn.bdqn.dto.questionBank.test_paper.TestPaperInsertSubjectIdDTO;
import cn.bdqn.dto.questionBank.test_paper.TestPaperPageDTO;
import cn.bdqn.service.questionBank.AccountTypeService;
import cn.bdqn.service.questionBank.TestPaperService;
import cn.bdqn.service.questionBank.TestPaperTypeService;
import cn.bdqn.utils.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/testPaper")
public class TestPaperController {

    @Autowired
    private TestPaperService testPaperService;

    @Autowired
    private AccountTypeService accountTypeService;
    @Autowired
    private TestPaperTypeService testPaperTypeService;
    /**
     * 分页查询
     * @param testPaperPageDTO
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/selectPageTestPaper")
    public Json selectPageTestPaper(TestPaperPageDTO testPaperPageDTO){
        System.out.println(testPaperPageDTO);
        return testPaperService.selectPageTestPaper(testPaperPageDTO);
    }

    /**
     * 初始化页面
     * @param testPaperPageDTO
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/initializationTestPaperPage")
    public Json initializationTestPaperPage(TestPaperPageDTO testPaperPageDTO){
        Json json = selectPageTestPaper(testPaperPageDTO);
        Map<String,Object> map = new HashMap<>();
        map.put("test_paper",json.getData());
        map.put("account",accountTypeService.selectIndustryAccountType());
        map.put("test_paper_type",testPaperTypeService.selectAllTestPaperType().getData());
        return Json.ok(map);
    }

    /**
     * 获取试卷题目
     * @param testPaperId
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/selectSubjectByTestPaperId")
    public Json selectSubjectByTestPaperId(Integer testPaperId){
        return testPaperService.selectSubjectByTestPaperId(testPaperId);
    }

    /**
     * 添加试卷
     * @param servletRequest
     * @param testPaperInsertDTO
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @PostMapping("/insertTestPaper")
    public Json insetTestPaper(HttpServletRequest servletRequest, @RequestBody TestPaperInsertDTO testPaperInsertDTO){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        try {
            //String idString = JwtUtil.parseJWT(httpServletRequest.getHeader("token")).getSubject();
            return testPaperService.insertTestPaper(userid,testPaperInsertDTO);
        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }
    }

    /**
     * 题目和试卷发布未发布的数量
     * @return
     */
    @GetMapping("/selectCountNumber")
    public Json selectCountNumber(){
        return testPaperService.selectCountNumber();
    }

    /**
     * 添加试卷，用户自己添加题目
     * @param testPaperInsertSubjectIdDTO
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @PostMapping("/insertTestPaperBySubjectListId")
    public Json insertTestPaperBySubjectListId(@RequestBody TestPaperInsertSubjectIdDTO testPaperInsertSubjectIdDTO){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        try {
            return testPaperService.insertTestPaperBySubjectListId(userid,testPaperInsertSubjectIdDTO);
        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param testPaperIdList
     * @param testPaperPageDTO
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @DeleteMapping("/deleteTestPaperList")
    public Json deleteTestPaperList(@RequestParam("testPaperIdList") String testPaperIdList, TestPaperPageDTO testPaperPageDTO){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        List<Integer> list = new ArrayList<>();
        if(testPaperIdList != null && !"".equals(testPaperIdList)){
            for(String i:testPaperIdList.split(",")){
                list.add(Integer.parseInt(i));
            }
        }
        try {
            return testPaperService.deleteTestPaperList(userid,testPaperPageDTO,list);
        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }
    }
}
